package com.ywj.collection.Collections;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class ChangeArgs {
    public static void main(String[] args) {
//        int sum = getSum(8, 10, 5, 13, 2300);
//        System.out.println(sum);
        String date1 = "20240711";
        String date2 = "2024-07-11";
//        String date3 = null;
        System.out.println(diffDays(date1));
        System.out.println(diffDays(date2));
//        System.out.println(diffDays(date3));
        System.out.println(diffDays("20240601"));

    }

    public static int getSum(int... arr) {
        int sum = 0;
        for (int a : arr) {
            sum += a;
        }
        return sum;
    }

    //20240711  2024-07-11  null
    public static long diffDays(String expiredDateString) {
//        if (.isBlank())
        try {
            if (expiredDateString.contains("-")) {
                DateTimeFormatter formatterWithHyphens = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate expirationDate = LocalDate.parse(expiredDateString, formatterWithHyphens);
                LocalDate today = LocalDate.now();
                return ChronoUnit.DAYS.between(today, expirationDate);
            } else {
                DateTimeFormatter formatterWithoutHyphens = DateTimeFormatter.ofPattern("yyyyMMdd");
                LocalDate expirationDate = LocalDate.parse(expiredDateString, formatterWithoutHyphens);
                LocalDate today = LocalDate.now();
                return ChronoUnit.DAYS.between(today, expirationDate);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

}
